
const requests = require('../../../config/request');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    minusStatus: true,
    courseCount: 0,
    address: {
      id: '',
      username: '',
      cellphone: '',
      region: ''
    },
    is_address: false,
    order_sn: '',
    shop_id: '',
    sum: 0,
    shop: {
      "id": "",
      "rid": "",
      "shop_name": "",
      "img_path": "",
      "cate_id1": "",
      "cate_id2": "",
      "region_id1": "",
      "region_id2": "",
      "region_id3": "",
      "score": "",
      "shop_hours": "",
      "fee_start": 0,
      "fee_delivery": 0,
      "phone2": "",
      "fee_pack": 0
    },
    cart: { sum: 0, menu: [] },
    menu_other: [],
    remark: '',
    goods_info: {
      count: 0,
      price: 0,
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ shop_id: options.shop_id })
    this.confirm();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  redirectToAddress() {
    wx.navigateTo({
      url: './../../userAddress/addressList/addressList?from=orderSure',// 地址选着页面
      events: {
        selected: (address) => {
          this.setData({ address:address.address, is_address: true }) // 地址选着成功
        }
      }
    })
  },
  confirm: function () {
    wx.showLoading({ title: '加载中', })
    requests.confirmOrder({ shop_id: this.data.shop_id, address_id: this.data.address.id })
      .then(({ order_sn, sum, shop, cart, menu_other, address }) => {
        Array.isArray(menu_other) && menu_other.map((item) => {
          item.num = 0
        })
        this.setData({
          order_sn, sum, shop, cart, menu_other
        })
        this.setData({
          address: {
            id: address.id,
            username: address.name,
            cellphone: address.phone,
            region: address.address
          },
          is_address: address.id != 0,
        })
        this.priceChange();
        wx.hideLoading();
      })
  },

  menuOtherNumberChange(e) {
    let index = e.currentTarget.dataset.index;
    if (!this.data.menu_other[index]) {
      return;
    }
    let num = e.currentTarget.dataset.number;
    if (num <= 0) {
      num = 0
    }
    let menu_other = this.data.menu_other;
    menu_other[index].num = num;
    this.setData({ menu_other })

    this.priceChange();

  },

  changeRemark(e) {
    this.data.remark = e.detail.value;
  },
  submitOrder() {
    if (!this.data.is_address) {
      wx.showToast({
        title: '请选择配送地址',
      })
      return;
    }
    wx.showLoading({ title: '提交中' })
    const menu_other = [];

    Array.isArray(this.data.menu_other) && this.data.menu_other.forEach((item) => {
      if (item.num > 0) {
        menu_other.push({ id: item.id, num: item.num })
      }
    })
    requests.submitOrder({
      shop_id: this.data.shop_id,
      address_id: this.data.address.id,
      // address_id: 'D995A1CC-0C40-6893-1CBE-87359620BA3F',
      order_sn: this.data.order_sn,
      menu_other: JSON.stringify(menu_other),
      remark: this.data.remark
    }).then(({ order_sn, goods_name, shop_name, money }) => {
      wx.hideLoading()
      this.redirectToPay(order_sn, goods_name, shop_name, money)
    })
  },
  redirectToUploadImage() {
    wx.navigateTo({
      url: '../../uploadImg/uploadImg?isUser=1&id=' + this.data.order_sn, // 跳转文件上传
    });
  },
  redirectToPay(order_sn, goods_name, shop_name, money) {
    // 跳转去支付
    wx.redirectTo({
      url: '../../payOrder/payOrder?orderId=' + order_sn + "&money=" + money + "&goods_name=" + goods_name + "&shop_name=" + shop_name
    });
  },
  priceChange() {
    let count = this.data.shop.fee_delivery * 1 + this.data.shop.fee_pack;
    let goods_number = 0;
    // 购物车价钱
    Array.isArray(this.data.cart.menu) && this.data.cart.menu.forEach((item) => {
      count += item.num * item.price_shop;
      goods_number++;
    });

    this.setData({
      goods_info: Object.assign(this.data.goods_info, {
        price: parseFloat(count).toFixed(2),
        count: goods_number,
      })
    })

    // 计算代取件加个
    Array.isArray(this.data.menu_other) && this.data.menu_other.forEach((item) => {
      count += item.price_shop * (item.num <= 0 ? 0 : item.num - 1);
    });

    this.setData({ count: parseFloat(count).toFixed(2) });
  },
  test() {
    console.log(arguments)
  }
})